package cn.yitun.chat.yitunserverimapicore.business.remote.action;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.impl.HeadImpl;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitungeneral.annotation.action.ActionMapping;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.remote.RemotePushDto;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimcore.general.main.push.remote.RemotePush;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 
 * @author yitun
 *
 */
@Component
@ActionMapping(value = ActionTypeConstant.REMOTE_ACTION_PUSH)
public class RemoteAction {

	@Resource
	RemotePush remotePush;

	/**
	 * 获取远程协助服务地址
	 * @author: yitun
	 * @createDate: 2017年4月14日 上午9:16:47
	 */
	@ActionMapping(value = "1.1.0001")
	public ResultMessage getRemoteServerPort() {

		return new ResultMessage(R.param("remoteAddress", null));
	}

	@ActionMapping(value = "1.1.0003")
	public ResultMessage getUserRemoteAddress(@JsonDefine("data") RemotePushDto query) {
		if (NumUtil.isInValid(query.getUserId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		return new ResultMessage(R.param("remoteAddress", null));
	}

	/**
	 * 请求控制对方电脑
	 * @author: yitun
	 * @createDate: 2017年4月14日 上午9:17:15
	 */
	@ActionMapping(value = "1.1.0004")
	public void remoteControlRequest(HeadImpl head, @JsonDefine("data") RemotePushDto query) {
		if (NumUtil.isInValid(query.getReceiveUserId()) || NumUtil.isInValid(query.getSendUserId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		remotePush.pushRequestRemoteControl(head.getKey(), query.getReceiveUserId(), query.getSendUserId());
	}

	/**
	 * 回应对方请求控制电脑
	 * @author: yitun
	 * @createDate: 2017年4月14日 上午9:17:30
	 */
	@ActionMapping(value = "1.1.0005")
	public void remoteControlResponse(HeadImpl head, @JsonDefine("data") RemotePushDto query) {

		if (NumUtil.isInValid(query.getReceiveUserId()) || NumUtil.isInValid(query.getSendUserId())
				|| YitunStringUtil.isEmpty(query.getActionType())
				|| YitunStringUtil.isEmpty(query.getCode())
				|| YitunStringUtil.isEmpty(query.getMessage()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		remotePush.pushResponseRemoteControl(head.getKey(),
				query.getSendUserId(),
				query.getReceiveUserId(),
				query.getActionType(),
				query.getCode(),
				query.getMessage());
	}

	@ActionMapping(value = "1.1.0006")
	public void remoteAssistRequest(HeadImpl head, @JsonDefine("data") RemotePushDto query) {

		if (NumUtil.isInValid(query.getReceiveUserId()) || NumUtil.isInValid(query.getSendUserId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		remotePush.pushRequestRemoteAssist(head.getKey(), query.getReceiveUserId(), query.getSendUserId());
	}

	@ActionMapping(value = "1.1.0007")
	public void remoteAssistResponse(HeadImpl head, @JsonDefine("data") RemotePushDto query) {
		if (NumUtil.isInValid(query.getReceiveUserId()) || NumUtil.isInValid(query.getSendUserId())
				|| YitunStringUtil.isEmpty(query.getActionType())
				|| YitunStringUtil.isEmpty(query.getCode())
				|| YitunStringUtil.isEmpty(query.getMessage()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		remotePush.pushResponseRemoteAssist(head.getKey(), query.getSendUserId(),
				query.getReceiveUserId(),
				query.getActionType(),
				query.getCode(),
				query.getMessage());
	}

	@ActionMapping(value = "1.1.0008")
	public void releaseRemoteControl(HeadImpl head, @JsonDefine("data") RemotePushDto query) {
		if (NumUtil.isInValid(query.getReceiveUserId()) || NumUtil.isInValid(query.getSendUserId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		remotePush.pushReleaseRemoteControl(head.getKey(), query.getReceiveUserId(), query.getSendUserId());
	}
}
